package com.cloud.basic.Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @program: basic
 * @description: jdk动态代理
 * @author: Cloud
 * @create: 2020/11/25 14:35:59
 */
public class jdkProxy implements InvocationHandler {

    // 这里是一个需要代理的目标类
    private Object target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk动态代理开始！");
        Object invoke = method.invoke(target, args);
        System.out.println("jdk动态代理结束！");
        return invoke;
    }

    // 定义获取代理对象方法
    private Object getProxyProject(Object targetObject){
        this.target = targetObject;
        //JDK动态代理只能针对实现了接口的类进行代理，newProxyInstance 函数所需参数就可看出
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    public static void main(String[] args) {
        jdkProxy jdkProxy = new jdkProxy();
        SysUserManager proxyProject = (SysUserManager) jdkProxy.getProxyProject(new SysUserManagerImp());
        proxyProject.addUser("张三","打羽毛球","程序员");
    }
}
